﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes" version="1.0" encoding="US-ASCII" standalone="no" />

  <xsl:template match="/">
    <xsl:processing-instruction name="OFX">
      <xsl:text>OFXHEADER="200" VERSION="200" SECURITY="NONE" OLDFILEUID="NONE" NEWFILEUID="NONE"</xsl:text>
    </xsl:processing-instruction>

    <OFX>
      <SIGNONMSGSRSV1>
        <SONRS>
          <STATUS>
            <CODE>0</CODE>
            <SEVERITY>INFO</SEVERITY>
          </STATUS>
          <DTSERVER>
            <xsl:call-template name="iso-date-time-stamp">
              <xsl:with-param name="date-time" select="Statement/Date" />
            </xsl:call-template>
          </DTSERVER>
          <LANGUAGE>ENG</LANGUAGE>
        </SONRS>
      </SIGNONMSGSRSV1>

      <CREDITCARDMSGSRSV1>
        <CCSTMTTRNRS>
          <TRNUID>0</TRNUID>
          <STATUS>
            <CODE>0</CODE>
            <SEVERITY>INFO</SEVERITY>
          </STATUS>
          <CCSTMTRS>
            <CURDEF>
              <xsl:value-of select="Statement/Currency" />
            </CURDEF>
            <CCACCTFROM>
              <ACCTID>
                <xsl:value-of select="Statement/Account/Number" />
              </ACCTID>
            </CCACCTFROM>

            <BANKTRANLIST>
              <DTSTART>
                <xsl:call-template name="iso-date-time-stamp">
                  <xsl:with-param name="date-time" select="Statement/DateStart" />
                </xsl:call-template>
              </DTSTART>
              <DTEND>
                <xsl:call-template name="iso-date-time-stamp">
                  <xsl:with-param name="date-time" select="Statement/DateEnd" />
                </xsl:call-template>
              </DTEND>

              <xsl:apply-templates select="Statement//StatementLines/StatementLine" />

            </BANKTRANLIST>

            <LEDGERBAL>
              <BALAMT>
                <xsl:call-template name="number">
                  <xsl:with-param name="value" select="string(Statement/Account/Balance)" />
                </xsl:call-template>
              </BALAMT>
              <DTASOF>
                <xsl:call-template name="iso-date-time-stamp">
                  <xsl:with-param name="date-time" select="Statement/DateEnd" />
                </xsl:call-template>
              </DTASOF>
            </LEDGERBAL>

            <AVAILBAL>
              <BALAMT>
                <xsl:call-template name="number">
                  <xsl:with-param name="value" select="string(-number(Statement/Account/SpendingLimit) - number(Statement/Account/Balance))" />
                </xsl:call-template>
              </BALAMT>
              <DTASOF>
                <xsl:call-template name="iso-date-time-stamp">
                  <xsl:with-param name="date-time" select="Statement/DateEnd" />
                </xsl:call-template>
              </DTASOF>
            </AVAILBAL>

          </CCSTMTRS>
        </CCSTMTTRNRS>
      </CREDITCARDMSGSRSV1>
    </OFX>
  </xsl:template>

  <xsl:template match="StatementLine">
    <STMTTRN>
      <TRNTYPE>
        <xsl:apply-templates select="TransactionType" />
      </TRNTYPE>
      <DTPOSTED>
        <xsl:call-template name="iso-date-time-stamp">
          <xsl:with-param name="date-time" select="DatePosted" />
        </xsl:call-template>
      </DTPOSTED>
      <TRNAMT>
        <!--<xsl:text>-</xsl:text>-->
        <xsl:choose>
          <xsl:when test="Comissions">
            <xsl:call-template name="number">
              <xsl:with-param name="value" select="string(Amount + Comissions)" />
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="number">
              <xsl:with-param name="value" select="string(Amount)" />
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </TRNAMT>
      <FITID>
        <xsl:value-of select="TransactionId" />
      </FITID>
      <NAME>
        <xsl:value-of select="substring(Description, 1, 32)" />
      </NAME>
      <xsl:if test="Memo">
        <MEMO>
          <xsl:value-of select="substring(Memo, 1, 256)" />
        </MEMO>
      </xsl:if>
    </STMTTRN>
  </xsl:template>

  <xsl:template name="iso-date-time-stamp">
    <xsl:param name="date-time" />

    <xsl:if test="$date-time">
      <xsl:value-of select="substring($date-time, 1, 4)" />
      <xsl:value-of select="substring($date-time, 6, 2)" />
      <xsl:value-of select="substring($date-time, 9, 2)" />
      <xsl:value-of select="substring($date-time, 12, 2)" />
      <xsl:value-of select="substring($date-time, 15, 2)" />
      <xsl:value-of select="substring($date-time, 18, 2)" />
      <xsl:text>[</xsl:text>
      <xsl:value-of select="substring($date-time, string-length($date-time) - 5, 3)" />
      <xsl:text>:GMT]</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template name="number">
    <xsl:param name="value" />

    <xsl:value-of select="translate($value, '.', ',')" />
  </xsl:template>

  <xsl:template match="TransactionType">
    <xsl:choose>
      <xsl:when test=".='Payment'">
        <xsl:text>PAYMENT</xsl:text>
      </xsl:when>
      <xsl:when test=".='ServiceCharge'">
        <xsl:text>SRVCHG</xsl:text>
      </xsl:when>
      <xsl:when test=".='Fee'">
        <xsl:text>FEE</xsl:text>
      </xsl:when>
      <xsl:when test=".='DirectDeposit'">
        <xsl:text>DIRECTDEP</xsl:text>
      </xsl:when>
      <xsl:when test=".='Transfer'">
        <xsl:text>XFER</xsl:text>
      </xsl:when>
      <xsl:when test=".='Credit'">
        <xsl:text>CREDIT</xsl:text>
      </xsl:when>
      <xsl:when test=".='Debit'">
        <xsl:text>DEBIT</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>POS</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
